package com.yehebl.orm.config;

import java.lang.management.ManagementFactory;
import java.util.List;
import java.util.UUID;

import com.yehebl.orm.enums.*;
import com.yehebl.orm.util.DateUtil;
import com.yehebl.orm.util.IdWorker;
import org.springframework.jdbc.core.JdbcTemplate;

import com.yehebl.orm.DsMain;
import com.yehebl.orm.data.common.DsJdbcTemplate;
import com.yehebl.orm.data.common.IDao;
import com.yehebl.orm.data.common.IJdbc;
import com.yehebl.orm.data.mysql.MySqlDao;
import com.yehebl.orm.util.LogUtil;


/**
 * 默认实现的框架配置
 */
public abstract class AbstractDsConfig implements IConfig {


    private IdWorker idWorker = new IdWorker();

    public AbstractDsConfig() {

    }

    public AbstractDsConfig(IJdbc jdbc) {
        setJdbc(jdbc);
    }

    private IJdbc jdbc;

    @Override
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        setJdbc(new DsJdbcTemplate(jdbcTemplate));
    }

    public void setJdbc(IJdbc jdbc) {
        this.jdbc = jdbc;
        DsMain.loadConfig(this);
    }

    public void setJdbcNoInit(IJdbc jdbc) {
        this.jdbc = jdbc;
    }

    private IDao dao;

    @Override
    public String getScanXml() {
        return null;
    }

    @Override
    public String classToTableRule(Class<?> clazz) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < clazz.getSimpleName().length(); i++) {
            char temp = clazz.getSimpleName().charAt(i);
            if (i != 0 && Character.isUpperCase(temp)) {
                sb.append("_");
            }
            sb.append(temp);
        }
        return sb.toString().toLowerCase();
    }

    @Override
    public String fieldToColumnRule(Class<?> clazz, String fieldName) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < fieldName.length(); i++) {
            char temp = fieldName.charAt(i);
            if (Character.isUpperCase(temp)) {
                sb.append("_");
            }
            sb.append(temp);
        }
        return sb.toString().toLowerCase();
    }

    @Override
    public SqlLogMode getSqlLogMode() {
        return SqlLogMode.NONE;
    }

    @Override
    public SqlLogMode getDebugSqlLogMode() {
        return SqlLogMode.ALL_FORMAT;
    }

    @Override
    public final SqlLogMode getSqlLogModeByRunMode() {
        if (RunMode.RUN.equals(getRunMode())) {
            return getSqlLogMode();
        } else {
            return getDebugSqlLogMode();
        }
    }

    private RunMode runMode = null;

    @Override
    public RunMode getRunMode() {
        if (runMode != null) {
            return runMode;
        }
        List<String> args = ManagementFactory.getRuntimeMXBean().getInputArguments();
        for (String arg : args) {
            if (arg.startsWith("-Xrunjdwp") || arg.startsWith("-agentlib:jdwp")) {
                runMode = RunMode.DEBUG;
                return runMode;
            }
        }
        runMode = RunMode.RUN;
        return runMode;
    }

    @Override
    public DataType getDataType() {
        return DataType.MYSQL;
    }

    @Override
    public void printSql(String sql) {
        LogUtil.printInfo(sql);
    }

    @Override
    public Object getAutoId(IdType idType) {
        switch (idType) {
            case uuid:
            case auto:
                return UUID.randomUUID().toString().replace("-", "");
            case snowflake:
                return getSnowflakeId();
            default:
                break;
        }
        return null;
    }

    @Override
    public IdType getDefaultIdType() {
        return IdType.uuid;
    }

    @Override
    public IDao getDao() {
        if (getDataType() == DataType.MYSQL) {
            if (dao == null) {
                dao = new MySqlDao(this);
            }
            return dao;
        }
        return null;
    }

    @Override
    public final IJdbc getJdbc() {
        return jdbc;
    }


    @Override
    public long getSnowflakeId() {
        return getIdWorker().nextId();
    }


    /**
     * id 生成器  继承IdWorker后可以复写 nextId()
     *
     * @return
     */
    public IdWorker getIdWorker() {
        return idWorker;
    }

    /**
     * 根据规则获取新的表名
     *
     * @param tableNameRule
     * @param tableName
     * @return
     */
    public String getTableRuleName(TableNameRule tableNameRule, String tableName) {
        switch (tableNameRule) {
            case day:
                return tableName + "_" + DateUtil.getDateDay();
            case month:
                return tableName + "_" + DateUtil.getDateMonth();
            case year:
                return tableName + "_" + DateUtil.getDateYear();
        }
        return tableName;

    }

}
